<?php
Class OrderModel extends Model{
	public function __construct(){
		$this->open(Model::MYSQL);
		$this->calledClass(get_class($this));
	}
	public function queryJoinTable(){
		$query = "SELECT Currencies.symbol, Currencies.id, Orders.item, Orders.currency_id, Orders.type, Orders.amount,
				  Orders.buy_date, Orders.`status`, Orders.emp_id, Orders.id
				  FROM Currencies
				  INNER JOIN Orders ON Currencies.id = Orders.currency_id";
		return $query;
	}
	/*with WHERE word*/
	public function DisplayAdmin($query,$page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "$query WHERE" ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= implode(" $operation ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	/*Multi table with find as object*/
	public function GetFieldWithMultiTable($query, $where=array()){
		$this->open(Model::MYSQL);
		if(is_array($where)&&!empty($where)){
			$query = " $query " ;
			$temp=array();
			foreach($where as $fieldName => $fieldValue ){
				$temp[] = $fieldName . "'" . mysql_real_escape_string( $fieldValue ) . "' " ;
			}
			$query .= " WHERE " . implode( " AND " , $temp ) . " ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return $result===null?$result:(empty($result)?null:$result[0]);
		}
	}
	
	public function QueryPrice(){
		$this->open(Model::MYSQL);
		$query = "SELECT Packages.price, Currencies.symbol
				FROM
				Currencies
				INNER JOIN Packages ON Currencies.id = Packages.currency_id";
		$query = parent::query($query);
		while (($row = mysql_fetch_object($query))!=false) {
			$result[]=$row;
		}
		return $result;
	}
	public function QueryCurrency(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Currencies ";
		$query = parent::query($query);
		while (($row = mysql_fetch_object($query))!=false) {
			$result[]=$row;
		}
		return $result;
	}
	public function DisplayOrder($where){
		$this->open(Model::MYSQL);
		$query = "SELECT Orders.item, Orders.id, Currencies.id, Currencies.symbol, Orders.amount, 
				  Orders.buy_date, Orders.`status` FROM Orders
				  INNER JOIN Currencies ON Currencies.id = Orders.currency_id
				  ORDER BY Orders.id DESC LIMIT 1";
		$result = parent::query($query) ;
		$datas = array();
		while(($row = mysql_fetch_object($result))!=false){
			$datas[] = $row ;
		}
		return $datas;
	}
	public function UpdateStatus($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields, $where) ;
	}
	public function InsertOrder($emp_id,$field){
		$this->open(Model::MYSQL);
		$type =  ucwords($_SESSION['type']);
		$queryorder = "INSERT INTO Orders (item_id,item,type,amount,buy_date,status,emp_id,currency_id, version,created,point,valid_day) VALUES ($field->id,'$field->title','$type',$field->price,NOW(),0,$emp_id,$field->currency_id,1,NOW(),$field->point,$field->valid_day) ;";		
		parent::query($queryorder);
		return $field;
	}
	public function InsertOrderSub($emp_id,$field){
		$this->open(Model::MYSQL);
		$type =  ucwords($_SESSION['type']);
		$queryorder = "INSERT INTO Orders (item_id,item,type,amount,buy_date,status,emp_id,currency_id, version,created,valid_day) VALUES ($field->id,'$field->title','$type',$field->price,NOW(),0,$emp_id,$field->currency_id,1,NOW(),$field->duration) ;";
		parent::query($queryorder);
		return $field;
	}
	public function delete($where=array(),$deleteSet=false){
		$this->open(Model::MYSQL);
		return parent::delete($where,$deleteSet);
	}
	public function edit($fields=array(),$wheres=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields,$wheres);
	}
	public function findObject($wheres=array()){
		$this->open(Model::MYSQL);
		return parent::findAsObject($wheres);
	}
	public function version($field,$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue($field,$where);
	}
	public function QueryOrder($type){
		$query = "SELECT Currencies.symbol, Orders.item, Orders.currency_id, Orders.type, Orders.amount,
					Orders.buy_date,Orders.point,Orders.valid_day, Orders.`status`, Orders.emp_id, Orders.id, Employers.company_name
					FROM Currencies INNER JOIN Orders ON Currencies.id = Orders.currency_id
					INNER JOIN Employers ON Employers.id = Orders.emp_id WHERE type = '".$type."'";
		return $query;
	}
	public function AdminOrder($query,$page=1,$perpage=20,$search ="",$searchFields = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = "$query" ;
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " $field LIKE '%".$search."%' " ;
			}
			$query .= " AND" . implode(" OR ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	/*without WHERE word*/
	public function DisplayAdminOrder($query,$page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "$query" ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " $key LIKE '%". $val ."%' " ;
			}
			$query .= " AND" . implode(" $operation ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function queryPriceFromPackage(){
		$this->open(Model::MYSQL);
		$query = "SELECT price FROM Packages";
		$result = parent::query($query) ;
		$datas = array();
		while(($row = mysql_fetch_object($result))!=false){
			$datas[] = $row ;
		}
		return $datas;
	}
	public function queryPriceFromSubscription(){
		$this->open(Model::MYSQL);
		$query = "SELECT price FROM Subscriptions";
		$result = parent::query($query) ;
		$datas = array();
		while(($row = mysql_fetch_object($result))!=false){
			$datas[] = $row ;
		}
		return $datas;
	}
	public function GetOrderByType($type){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Orders WHERE type = '$type' " ;
		$result = parent::query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
	public function getPage($user_id,$type,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT * FROM Orders WHERE emp_id = $user_id AND type = '$type'" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
}